package FeedMaker;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.os.Environment;

public class DdanjiEntrys
{
	private Hashtable< String, Hashtable< String, DdanjiEntry > > entrys_;
	private String save_path_;
	
	DdanjiEntrys( String save_path )
	{
		entrys_ = new Hashtable< String, Hashtable< String, DdanjiEntry > >();
		save_path_ = save_path;
		load();
	}
	
	synchronized public void add_impl( String feed, DdanjiEntry entry )
	{
		Hashtable< String, DdanjiEntry > elem = entrys_.get(feed);
		
		if( elem == null )
		{
			elem = new Hashtable< String, DdanjiEntry >();
			elem.put( entry.title_, entry );
			entrys_.put( feed, elem );
		}
		else
		{
			elem.put( entry.title_, entry );
		}
	}
	
	synchronized public void add( String feed, DdanjiEntry entry ) 
	{
		add_impl( feed, entry );
		save();
	}
	
	synchronized public int entry_count( String feed )
	{
		Hashtable< String, DdanjiEntry > elem = entrys_.get(feed);
		
		return elem != null ? elem.size() : 0;
	}
	
	synchronized public DdanjiEntry get( String feed, String title )
	{
		DdanjiEntry result = null;
		
		Hashtable< String, DdanjiEntry > elem = entrys_.get(feed);
		
		if( elem != null )
		{
			result = elem.get(title);
		}
		
		return result == null ? result : new DdanjiEntry(result);
	}
	
	synchronized public void get_elements( String feed, List<DdanjiEntry> entrys ) 
	{
		if( feed != null && entrys != null )
		{
			entrys.clear();
			
			Hashtable<String, DdanjiEntry> elem = entrys_.get(feed);
			
			if( elem != null )
			{
				entrys.addAll( elem.values() );
				
				Collections.sort(entrys, new Comparator<DdanjiEntry>(){
					  public int compare( DdanjiEntry e1, DdanjiEntry e2)
					  {
						  if( e1.number_ < e2.number_ )
							  return 1;
						  if( e1.number_ == e2.number_ )
							  return 0;
						  
						  return -1;
					  }
				});
			}
		}
	}
	
	synchronized public void save()
	{		
		XmlWriter writer = new XmlWriter();
		
		if( writer.doc_ != null )
		{
			Element root = writer.doc_.createElement( "entrys" );
			
			writer.doc_.appendChild(root);
			
			for( Enumeration< String > f = entrys_.keys(); f.hasMoreElements(); )
			{
				String feed = f.nextElement();
				
				Element feed_elem = writer.doc_.createElement( "feed" );
				
				feed_elem.setAttribute( "title", feed);
				
				Hashtable< String, DdanjiEntry > feed_entrys = entrys_.get(feed);
				
				root.appendChild( feed_elem );
				
				for( Enumeration<DdanjiEntry> i = feed_entrys.elements(); i.hasMoreElements(); )
				{
					DdanjiEntry e = i.nextElement();
					
					Element entry = writer.doc_.createElement( "entry" );
					entry.setTextContent( e.toData() );
					feed_elem.appendChild(entry);
				}
			}
			
			String filename = Environment.getExternalStorageDirectory() + "/" + save_path_ + "ddanji_feed_entrys.xml";
			
			File file = new File( filename );
			
			if( file.isFile() )
				file.delete();
			
			writer.save( filename );
		}
	}
	
	synchronized public void load()
	{
		entrys_.clear();
		
		XmlParser parser = new XmlParser( Environment.getExternalStorageDirectory() + "/" + save_path_ + "ddanji_feed_entrys.xml" );
		
		if( parser.doc_ != null )
		{
			NodeList feedNodes = parser.doc_.getElementsByTagName( "feed" );
			
			for( int f=0; f<feedNodes.getLength(); ++f )
			{
				Node feedNode = feedNodes.item(f);
				
				NamedNodeMap attributes = feedNode.getAttributes();
				
				Node title_value = attributes.getNamedItem( "title" );
				
				String feed = "";
				
				if( title_value != null )
					feed = title_value.getNodeValue();
				
				NodeList feedEntrys = feedNode.getChildNodes();
				
				for( int i=0; i<feedEntrys.getLength(); ++i )
				{
					Node entry = feedEntrys.item(i);
					
					if( entry.getNodeName().equalsIgnoreCase( "entry" ) )
					{
						String data = entry.getTextContent();
						
						if( data != null && data.length() > 0 )
						{
							DdanjiEntry e = new DdanjiEntry();
							
							e.fromData(data);
							
							if( e.title_.length() > 0 )
							{
								add_impl( feed, e );
							}	
						}
					}
				}
				
				//ReIndexing( feed );
			}
		}
	}
	
	synchronized void ReIndexing( String feed )
	{
		Hashtable< String, DdanjiEntry > entrys = entrys_.get(feed);
		
		if( entrys != null )
		{
			List< DdanjiEntry > sortList = new ArrayList< DdanjiEntry >();
			sortList.addAll( entrys.values() );
			
			Collections.sort(sortList, new Comparator<DdanjiEntry>(){
				  public int compare( DdanjiEntry e1, DdanjiEntry e2)
				  {
					  if( e1.number_ < e2.number_ )
						  return -1;
					  if( e1.number_ == e2.number_ )
						  return 0;
					  
					  return 1;
				  }
			});
			
			int count=0;
			
			for( DdanjiEntry entry : sortList )
			{
				entry.number_ = count++;
			}
		}
	}
	
	synchronized void clearOld( String feed, HashSet<String> new_titles )
	{
		Hashtable<String, DdanjiEntry > entrys = entrys_.get(feed);
		
		HashSet< String > delete_title = new HashSet<String>(); 
		
		if( entrys != null )
		{
			for( Enumeration< String > i = entrys.keys(); i.hasMoreElements(); )
			{
				String title = i.nextElement();
				
				if( new_titles.contains( title ) == false )
				{
					delete_title.add(title);
				}
			}
			
			for( String title : delete_title )
			{
				entrys.remove( title );
			}
			
			ReIndexing( feed );
		}
	}
}